import { getToken, setToken, removeToken } from '@/utils/auth'
import { getUserProfile, getUserByIdDetail } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updateToken(state, token) {
      state.token = token
      setToken(token)
    },
    delToken(state) {
      state.token = ''
      removeToken()
    },
    UpDdateserInfo(state, info) {
      state.userInfo = info
    }
  },
  actions: {
    async  getUserInfo(context) {
      // console.log(context, 'context')
      try {
        const res = await getUserProfile()
        // console.log(res)
        const info = await getUserByIdDetail(res.data.userId)
        // console.log(info)
        context.commit('UpDdateserInfo', { ...res.data, ...info.data })
      } catch (error) {
        console.log(error)
      }
    },
    logout(context) {
      context.commit('delToken')
      context.commit('UpDdateserInfo', {})
    }
  }
}
